package com.shadow.demo.excel.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shadow.demo.common.bo.R;
import com.shadow.demo.common.constants.ExtensionConstants;
import com.shadow.demo.common.constants.PunctuationConstants;
import com.shadow.demo.data.excel.entity.EasyExcelEntity;
import com.shadow.demo.excel.service.DemoExcelService;
import com.shadow.demo.excel.demo.v2.listener.EasyExcelListener;
import com.shadow.demo.excel.demo.v2.vo.EasyExcelExportVO;
import com.shadow.demo.excel.demo.v2.vo.EasyExcelImportVO;
import com.shadow.demo.excel.service.EasyExcelService;
import com.shadow.demo.excel.easy.excel.EasyExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("excel")
public class ExcelController {

    /** 数据导出Excel名称. **/
    private static final String EXCEL_NAME       = "Default-";

    @Resource
    private EasyExcelService easyExcelService;
    @Resource
    private DemoExcelService demoExcelService;

    @GetMapping("test")
    public R test() {
        EasyExcelEntity easyExcelEntity = new EasyExcelEntity();
        easyExcelEntity.setId("1");
        LambdaUpdateWrapper<EasyExcelEntity> easyExcelEntityUpdateWrapper = new LambdaUpdateWrapper<>(EasyExcelEntity.class)
                .eq(EasyExcelEntity::getId, easyExcelEntity.getId())
                .set(EasyExcelEntity::getAge, 25);
        easyExcelService.update(easyExcelEntityUpdateWrapper);

        final List<EasyExcelExportVO> exportList = easyExcelService.getExportList(new HashMap<>());
        log.info(exportList.toString());
        return R.success(exportList);
    }



    /**
     * 数据excel导出.
     *
     * @param params 参数map
     * @param response 响应对象 1， 2.
     **/
    @GetMapping("exportData")
    public void exportData(@RequestParam final Map<String, Object> params, final HttpServletResponse response) {
        try {
            // 执行分页查询并写入数据到Excel导出
            final List<EasyExcelExportVO> exportList = easyExcelService.getExportList(params);
            EasyExcelUtil.exportData(response, EXCEL_NAME, null, null, EasyExcelExportVO.class,
                    exportList);
        } catch (IOException e) {
            log.error("exportData error. params : {}", params, e);
        }
    }

    /**
     * 数据excel导入.
     * version: 1
     *
     * @param excel excel
     **/
    @PostMapping("/importData")
    public R batchImport(final MultipartFile excel) {
        try {
            return easyExcelService.batchImport(excel.getInputStream());
        } catch (IOException e) {
            log.error("批量导入信息出错", e);
            return R.fail();
        }
    }

    /**
     * 数据excel导入.
     * version: 2
     *
     * @param excel excel
     **/
    @GetMapping("importData")
    public R importData(@RequestBody final MultipartFile excel) {
        // 无excel
        if (excel == null) {
            return R.fail("找不到文件");
        }
        // 文件名称
        final String filename = excel.getOriginalFilename();
        if (StringUtils.isBlank(filename) || filename.length() < 5) {
            return R.fail("文件名称有误");
        }
        // 后缀
        String extension = filename.substring(filename.lastIndexOf(PunctuationConstants.POINT) + 1);
        if (!StringUtils.equalsAnyIgnoreCase(extension, ExtensionConstants.XLS, ExtensionConstants.XLSX)) {
            return R.fail("文件格式有误");
        }
        try {
            final EasyExcelListener easyExcelListener = new EasyExcelListener(easyExcelService);
            String sss = EasyExcelUtil.importData(excel.getInputStream(), null, EasyExcelImportVO.class, easyExcelListener);
            if (StringUtils.isNotBlank(sss)) {
                return R.fail(sss);
            }
        } catch (Exception e) {
            log.error("exportData error. ", e);
        }
        return R.success();
    }

    /**
     * 数据excel导入.
     * version: 3
     *
     * @param excel excel
     **/
    @PostMapping("v3/demo/import")
    public R import2(@RequestBody final MultipartFile excel) {
        try {
            final String msg = demoExcelService.import3(excel);
            if (StringUtils.isNotEmpty(msg)) {
                return R.fail(msg);
            }
        } catch (Exception e) {
            log.error("exportData error. ", e);
            return R.fail("system error");
        }
        return R.success();
    }
}
